package com.example.rabbitmqextensions.config;

import com.example.rabbitmqextensions.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(Constants.DLX_QUEUE).build();
    }

    @Bean("dlxExchange")
    public DirectExchange dlxExchange() {
        return ExchangeBuilder.directExchange(Constants.DLX_EXCHANGE).durable(true).build();
    }

    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxExchange")DirectExchange exchange, @Qualifier("dlxQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx");
    }

    // 普通队列(绑定死信队列)
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.
                durable(Constants.NORMAL_QUEUE).
                deadLetterExchange(Constants.DLX_EXCHANGE).
                deadLetterRoutingKey("dlx").
                ttl(40000).
                build();
    }

    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).durable(true).build();
    }

    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalExchange")DirectExchange exchange, @Qualifier("normalQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("normal");
    }

    // 事务
    @Bean("transQueue")
    public Queue transQueue() {
        return QueueBuilder.durable(Constants.TRANS_QUEUE).build();
    }

    // 限流
    @Bean("qosExchange")
    public DirectExchange qosExchange() {
        return ExchangeBuilder.directExchange(Constants.QOS_EXCHANGE).durable(true).build();
    }

    @Bean("qosQueue")
    public Queue qosQueue() {
        return QueueBuilder.durable(Constants.QOS_QUEUE).build();
    }
    @Bean("qosBinding")
    public Binding qosBinding(@Qualifier("qosExchange")DirectExchange exchange, @Qualifier("qosQueue")Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("qos");
    }

    @Bean("quorumQueue")
    public Queue quormQueue() {
        return QueueBuilder.durable(Constants.QUORUM_QUEUE).quorum().build();
    }

    @Bean("clusterQueue")
    public Queue clusterQueue() {
        return QueueBuilder.durable(Constants.CLUSTER_QUEUE).quorum().build();
    }
}
